let express=require("express");
let studentService = require("../src/services/studentService")
let router= express.Router();
//引入上传文件的工具类
let {uploadFile} = require("../src/utils/upload");

//分页显示
router.get("/showstudentInfosByPage", async (req,res)=>{
  //获得请求的参数
  let current = parseInt(req.query.currentPage);
  let pageSize = parseInt(req.query.pageSize);
  let result  = await studentService.showStudentsInfoByPage(current,pageSize);
  res.json(result);
})
//添加
router.post("/addstudent",async(req,res)=>{
    let obj={};
    let result = await uploadFile(req);
    //console.log(result);
    let {name,age,gender,classid}=result.data;
     //截取url后面的头像的名字
     let photo =result.data.url.split("/")[2];
     obj={name,age,gender,classid,photo};
    //console.log("添加的学生===>>",obj);
    let data= await studentService.addStudentInfo(obj);
     if(data!={}){
       res.json({"code":0,"msg":"添加成功"});
     }else{
      res.json({"code":1,"msg":"添加失败"});
     }
  })

//删除
router.post("/delstudentInfo", async (req,res)=>{
    //获得学生的编号
    let studentId = req.body.studentid;
    let result = await studentService.deleteStudentInfo(studentId);
    res.json(result);
})
//查询详细
router.get("/showstudentInfoDetail",async (req,res)=>{
 //获得学生的编号
 let studentId = req.query.studentid;
 let result = await studentService.showStudentInfoDetail(studentId);
 console.log(result)
 res.json(result);
})
//修改
router.post("/updatestudentInfo",async (req,res)=>{
  //获得学生的编号
 let studentId = req.body.studentid;
 //封装一个学生对象
 let student={};
  student._id = studentId;
  student.name=req.body.name;
  student.age=req.body.age;
  student.gender=req.body.gender;
  student.classid=req.body.classid;

  let result = studentService.updateStudentInfo(student);
  res.json({"code":0,"msg":"修改成功"});
});
module.exports=router;

